package com.bwie.controller;

import com.alibaba.fastjson.JSON;
import com.alibaba.fastjson.JSONArray;
import com.alibaba.fastjson.JSONObject;
import com.alibaba.fastjson.TypeReference;
import com.bwie.pojo.User;
import com.bwie.service.UserService;
import com.bwie.util.PageUtil;
import com.bwie.vo.UserVo;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.data.redis.core.RedisTemplate;
import org.springframework.stereotype.Controller;
import org.springframework.ui.Model;
import org.springframework.web.bind.annotation.RequestBody;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RequestParam;
import org.springframework.web.bind.annotation.ResponseBody;

import java.text.SimpleDateFormat;
import java.util.Date;
import java.util.List;
import java.util.concurrent.TimeUnit;

/**
 * @author By:HDsir
 * @date 2022/4/8
 */
@Controller
public class UserController {
    @Autowired
    private UserService userService;
    @Autowired
    private RedisTemplate<String,String> redisTemplate;
    @RequestMapping("/list")
    public String list(Model model, UserVo userVo,
                       @RequestParam(value = "pageNum",defaultValue = "1")Integer pageNum,
                       @RequestParam(value = "pageSize",defaultValue = "3")Integer pageSize) {
            List<User> list = userService.findList(userVo);
            PageUtil pu = new PageUtil(pageNum + "", list.size(), pageSize);
            list = userService.findListPage(pu,userVo);
            model.addAttribute("list",list);
            model.addAttribute("pu",pu);
            model.addAttribute("vo",userVo);
            return "list";
    }

    @RequestMapping("/toadd")
    public String toAdd() {
        return "add";
    }

    @RequestMapping("/add")
    @ResponseBody
    public Integer add(User user) {
        user.setLockState(1);
        Date date = new Date();
        SimpleDateFormat sdf = new SimpleDateFormat("yyyy-MM-dd HH:mm:ss");
        String format = sdf.format(date);
        user.setRegisterDate(format);
        return userService.add(user);
    }

    @RequestMapping("/toupd")
    public String toupd(Model model,Integer uid) {
        User user = userService.findByUserId(uid);
        model.addAttribute("user",user);
        return "upd";
    }

    @RequestMapping("/upd")
    @ResponseBody
    public Integer upd(User user) {
        return userService.upd(user);
    }

    @RequestMapping("del")
    @ResponseBody
    public Integer del(Integer id) {
        return userService.del(id);
    }

    @RequestMapping("toInfo")
    public String toInfo(Integer id,Model model) {
        model.addAttribute("usersId",id);
        return "info";
    }

    @RequestMapping("info")
    @ResponseBody
    public User info(Integer id) {
        System.out.println("从redis里查..............");
        String info = redisTemplate.opsForValue().get("info" + id);
        if (info==null) {
            System.out.println("从数据库查.................");
            User user = userService.findByUserId(id);
            String xiangqing = JSONObject.toJSONString(user);
            redisTemplate.opsForValue().set("info"+id,xiangqing,5, TimeUnit.MINUTES);
            return user;
        }
        User user = JSONArray.parseObject(info, User.class);
        return user;
    }
}
